package cn.net.yugu.doraemon.tablestore.springboot;

import com.alicloud.openservices.tablestore.AsyncClient;
import com.alicloud.openservices.tablestore.ClientConfiguration;
import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.model.AlwaysRetryStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @author xiangzr
 * @version 2023/08/02 14:21:27:27
 */
@Slf4j
@AutoConfiguration
@ConditionalOnProperty(prefix = TableStoreAutoConfiguration.TABLESTORE_PRIFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class TableStoreAutoConfiguration {

    protected static final String TABLESTORE_PRIFIX = "tablestore";

    @ConfigurationProperties(prefix = TABLESTORE_PRIFIX)
    @Bean
    public TableStoreProperties tableStoreProperties() {
        return new TableStoreProperties();
    }

    @ConditionalOnMissingBean(value = SyncClient.class, name = "tableStoreSyncClient")
    @Bean
    public SyncClient tableStoreSyncClient(TableStoreProperties tableStoreProperties) {

        ClientConfiguration configuration = new ClientConfiguration();
        configuration.setConnectionTimeoutInMillisecond(tableStoreProperties.getConnectionTimeoutInMillisecond());
        configuration.setSocketTimeoutInMillisecond(tableStoreProperties.getSocketTimeoutInMillisecond());

        final AlwaysRetryStrategy retryStrategy = new AlwaysRetryStrategy();
        retryStrategy.setMaxRetryTimes(tableStoreProperties.getMaxRetryTimes());
        configuration.setRetryStrategy(retryStrategy);

        final String endpoint = tableStoreProperties.getEndpoint();
        final String accessKeyId = tableStoreProperties.getAccessKeyId();
        final String secretAccessKey = tableStoreProperties.getSecretAccessKey();
        final String instanceName = tableStoreProperties.getInstanceName();
        log.info("初始化TableStore SyncClient 客户端 instanceName {} endpoint {}", instanceName, endpoint);
        return new SyncClient(endpoint, accessKeyId, secretAccessKey, instanceName, configuration);
    }

    @ConditionalOnMissingBean(value = AsyncClient.class, name = "tableStoreAsyncClient")
    @Bean
    public AsyncClient tableStoreAsyncClient(TableStoreProperties tableStoreProperties) {
        ClientConfiguration configuration = new ClientConfiguration();
        configuration.setConnectionTimeoutInMillisecond(tableStoreProperties.getConnectionTimeoutInMillisecond());
        configuration.setSocketTimeoutInMillisecond(tableStoreProperties.getSocketTimeoutInMillisecond());

        final AlwaysRetryStrategy retryStrategy = new AlwaysRetryStrategy();
        retryStrategy.setMaxRetryTimes(tableStoreProperties.getMaxRetryTimes());
        configuration.setRetryStrategy(retryStrategy);

        final String endpoint = tableStoreProperties.getEndpoint();
        final String accessKeyId = tableStoreProperties.getAccessKeyId();
        final String secretAccessKey = tableStoreProperties.getSecretAccessKey();
        final String instanceName = tableStoreProperties.getInstanceName();

        log.info("初始化TableStore AsyncClient 客户端 instanceName {} endpoint {}", instanceName, endpoint);
        return new AsyncClient(endpoint, accessKeyId, secretAccessKey, instanceName, configuration);
    }
}
